#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#define MAX 100

int number;

pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex;
pthread_rwlock_t rwlock;

void* funcA_num(void* arg){
    for(int i=0;i<MAX;i++){
        pthread_rwlock_rdlock(&rwlock); 
        printf("read Thread %lu  number %d\n",pthread_self(),number); 
        pthread_rwlock_unlock(&rwlock);
        
        pthread_rwlock_wrlock(&rwlock);
        printf("write Thread %lu  number %d\n",pthread_self(),++number); 
        pthread_rwlock_unlock(&rwlock);
    }
    pthread_exit(NULL);
}

int main(){
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
    pthread_rwlock_init(&rwlock,NULL);
    pthread_t thread[5]={0};
    
    for(int i=0;i<5;i++){
        pthread_create(thread+i,NULL,funcA_num,NULL);
    }

    for(int i=0;i<5;i++){
        pthread_join(thread[i],NULL);
    }
    printf("Thread %lu  number %d\n",pthread_self(),number); 
    pthread_cond_destroy(&cond);
    pthread_rwlock_destroy(&rwlock);
    pthread_mutex_destroy(&mutex);
    return 0;
}

